package com.jbm.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import static com.jbm.db.MySqliOpenHe.OPEN_LOG;

/**
 * Created by 家帮忙 on 2016/11/15.
 */

public class MySqliUtlis {

    private final MySqliOpenHe mysqlis;
    private Context context;
    private SQLiteDatabase db;
    private static MySqliUtlis util;
    public static MySqliUtlis getInstance(Context context){
        if(util==null){
            util=new MySqliUtlis(context);
        }
        return util;
    }
    private MySqliUtlis(Context context) {
        this.context = context;
        mysqlis = new MySqliOpenHe(context);
        if (mysqlis != null)
            db = mysqlis.getWritableDatabase();
 }

    //存储数据开门数据
    public void Opendatakey(int login_id, int eguard_id, int egkey_id, int last_count, String open_date, String create_date,int is_open,int try_cnt,String phone_module,String open_type,String ord_date,String call_date,String error) {
        db.execSQL("insert into " + OPEN_LOG + " (login_id,eguard_id,egkey_id,last_count,open_date,create_date,is_open,try_cnt,phone_module,open_type,ord_date,call_date,error)values(?,?,?,?,?,?,?,?,?,?,?,?,?)", new String[]{login_id + "", eguard_id + "", egkey_id + "", last_count + "", open_date, create_date,is_open+"",try_cnt+"",phone_module,open_type,ord_date,call_date,error});



    }



    //查找所有开门数据
    public ArrayList<MyOpenLog> ShowddataOpenKey() {
        ArrayList<MyOpenLog> list = new ArrayList<>();
        MyOpenLog data_open = null;
        Cursor cursor = db.rawQuery("select * from " + OPEN_LOG + "", null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            data_open = new MyOpenLog();
            data_open.setLogin_id(Integer.parseInt(cursor.getString(cursor.getColumnIndex("login_id"))));
            data_open.setEguard_id(Integer.parseInt(cursor.getString(cursor.getColumnIndex("eguard_id"))));
            data_open.setEgkey_id(Integer.parseInt(cursor.getString(cursor.getColumnIndex("egkey_id"))));
            data_open.setLast_count(Integer.parseInt(cursor.getString(cursor.getColumnIndex("last_count"))));
            data_open.setOpen_date(cursor.getString(cursor.getColumnIndex("open_date")));
            data_open.setCreate_date(cursor.getString(cursor.getColumnIndex("create_date")));
            data_open.setIs_open(Integer.parseInt(cursor.getString(cursor.getColumnIndex("is_open"))));
            data_open.setTry_cnt(Integer.parseInt(cursor.getString(cursor.getColumnIndex("try_cnt"))));
            data_open.setPhone_module(cursor.getString(cursor.getColumnIndex("phone_module")));
            data_open.setOpen_type(cursor.getString(cursor.getColumnIndex("open_type")));
            data_open.setOrd_date(cursor.getString(cursor.getColumnIndex("ord_date")));
            data_open.setCall_date(cursor.getString(cursor.getColumnIndex("call_date")));
            data_open.setError(cursor.getString(cursor.getColumnIndex("error")));
            list.add(data_open);
        }
        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;
    }

    // 删除open
    public void DeleteOp() {
        db.execSQL("delete  from " + OPEN_LOG + " ");
    }

//删除数据库
    public void DeleteBase(){
        db.execSQL("drop table " + OPEN_LOG+ "");
    }
}
